home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac: Not for Sale / Another.not.for.sale (Australia).iso / Dr. Doyle / C Lesson / C-LESSON.7 < prev    next >
Text File  |  1993-11-08  |  7KB  |  163 lines

  1.  
  2.  
  3. Lesson 6
  4.  
  5.           Libraries, why we have them, and how to make and use them.
  6.  
  7.   In order to simplify the creation of programs for its customers,
  8. software vendors make available one or more libraries of functions
  9. which have general application.
  10.  
  11.         When you write a program in 'C' much, if not most, of the "hard work"
  12. is done by explicitly called functions, which you call by simply writing
  13. their names in your program script. Unfortunately there is a little bit
  14. more to it than that. If the function returns a value which is other
  15. than of type int, you have to tell the compiler the type of the returned
  16. value. An example will, I hope make things clear.
  17.  
  18.         Lets's suppose that you have been given the task of, making things
  19. stupidly simplistic for a book example, sorting a list of names into
  20. alphabetical order. ( Yes, I do know this can, and should be done in just
  21. one line of shell script! However that's another story for another day. )
  22.  
  23.         You look diligently through the Programmer Reference Manual, discover
  24. that the prose is almost opaque, and find a couple of interesting looking
  25. routines called strcmp, and qsort. You decide to use these library
  26. functions. Now for just a moment lets consider the ins and outs of what,
  27. in effect, you have just done. You have just asked a member of the team
  28. of programmers who created the library to join you, by proxy as it were, in
  29. creating your masterpiece. A useful concept, which has been in use
  30. almost since the start of electronic computing.
  31.  
  32.         To re-focus the mind on the task at hand; let's look in the Reference
  33. Manual
  34. at the page for qsort(3C) - The 3C in parenthesis is the cryptic code which
  35. is the unix apology for a reference to section 3C in the Manual! So find
  36. section 3C and look up qsort. Now have a look at the SYNOPSIS, and notice
  37. that there is no mention of a header file to #include, and also notice that
  38. qsort returns a void, not an int. This means that there is no header file
  39. /usr/include/qsort.h ( for my version of unix - system V Release 3.2.2 -
  40. anyway ) and you have to declare qsort yourself as an external function.
  41. Also turn to the page string(3C) in the fine manual. Notice that the
  42. SYNOPSIS here includes the line #include <string.h> so you have to put
  43. it in your program text. Once more an example to make it all clear.
  44.  
  45.  
  46. /* ----------------------------------------- */
  47.  
  48. #ident "@(#) qsort-demo.c"
  49.  
  50. #include <stdio.h>
  51. #include <string.h>
  52. #include <assert.h>
  53.  
  54. extern void qsort ();
  55. extern int strcmp();        /* Some compilers need this defined, most don't. */
  56.  
  57. char names[22][25] =                         /* Here are some names to sort. */
  58. {
  59.   "John Nagle", "Colin Douthwaite", "Ian Lance Taylor", "Brian J. Murrell",
  60.   "Pete", "Geoff Mccaughan", "David Liebert", "Operator", "Bill Baucum",
  61.   "Victor Volkman", "Chay R Harley", "Dan Romanchik", "Larry Kollar",
  62.   "Gaston Ormazabal", "Arijit Chandra", "Kenneth Mark Hopkinson",
  63.   "Kerr Hatrick", "Tim Love", "Robert M. Juranitch", "Jeffrey Micke",
  64.   "Duong Quoc", "Jagadesh Vasudevamurthy"
  65.         };
  66.  
  67. #define NUMBER_OF_NAMES sizeof ( names ) / sizeof ( names[0] )
  68.  
  69. main()
  70. {
  71.   int i;
  72.  
  73.         /*
  74.         ** Print the unsorted names.
  75.         */
  76.  
  77.         printf ( "The Unsorted Names.\n" );
  78.   for ( i = 0; i < NUMBER_OF_NAMES; i++ ) printf ( "%s\n", names[i] );
  79.  
  80.         /*
  81.         ** Print a prompt, and wait.
  82.         */
  83.  
  84.         printf ( "Press RETURN to continue: " );
  85.         fflush ( stdout );
  86.         getchar();
  87.  
  88.         /*
  89.         ** Now apply qsort to the arrary of character strings.
  90.         */
  91.  
  92.         qsort (( char * ) names, NUMBER_OF_NAMES, sizeof ( *names ), strcmp );
  93.  
  94.         assert ( names[0][0] < names[1][0] );  /* Quick check to see it's done
  95. it. */
  96.  
  97.         /*
  98.         ** Print the sorted names.
  99.         */
  100.  
  101.         printf ( "The Sorted Names.\n" );
  102.   for ( i = 0; i < NUMBER_OF_NAMES; i++ ) printf ( "%s\n", names[i] );
  103.   }
  104.  
  105. /* ----------------------------------------- */
  106.  
  107.   Note very well:-
  108.  
  109.         I wanted 22 short character strings for the data items
  110. for the demo to sort. So grep, uniq, cut, tail, and finally a tiny bit of
  111. vi fished eminently suitable strings out of "mail.received". If your name
  112. is not on the list, well I'm sorry, but the world is not a fair place!
  113.  
  114.   So that's how you use library routines. I chose qsort because it is
  115. simple to use, and shows off a feature of 'C' well, that's the ability
  116. to use a name of a function as a pointer and then execute that function
  117. from within the called function. It's strcmp in this case. A quick look
  118. at the compiler output is instructive.
  119.  
  120.   As is the nature of the animal, a tin-pot little program, which should
  121. have taken all of ten minutes to get going in fact took more like two
  122. hours. I put it down to the fact that the Fine Manual did not make it
  123. adequately obvious that the data array acted on by qsort was the data itself.
  124. From reading the Fine Manual I got the impression that the array acted on
  125. was an array of pointers. You live and learn. It would be a much faster
  126. qsort if, in fact, the sorting function sorted pointers to data instead of
  127. the data itself. You might like to make a function qsort_p which worked in
  128. in this way. The qsort algorithm is well documented elsewhere.
  129.  
  130.         There is just one more point to notice about using function libraries.
  131. The 'C' compilation system will load functions from the library /lib/libc.a
  132. as a default. All others have to be indicated to the linking loader by a
  133. switch on the shell interactive command line.
  134.  
  135. $ cc -o prog prog.c -L /usr/local/lib -lgdbm -lmalloc
  136.  
  137.         You might use this command line to compile and link a program which
  138. uses both the GNU gdbm data-base manager library, which is installed in
  139. the directory /usr/local/lib, and the enhanced malloc library. Now, there
  140. hangs a tale! I remember having to compile a program suit off Usenet and
  141. it just would not work properly. No error messages, no warnings, no
  142. missing linking-loader symbols. It just "died" when I tried to run it.
  143. After many, many hours of total frustration, I thought that I would try
  144. linking in the enhanced malloc library. Presto! It worked.
  145.  
  146.         Note very well.
  147.  
  148.   A common misconception is the notion that having a #include <whatever.h>
  149. line in the source text will automagically tell the linking loader to
  150. get the functions from the appropriate library. Remove this erroroneous
  151. notion from your mind. It won't. The -lwhatever flag on the shell command
  152. line which initiates execution of "cc" or "ld" is the only way to tell the
  153. loader where to look for the required library.
  154.  
  155.  
  156. --
  157.  +----------------------------------------------------------------------+
  158.  | NAME   Christopher Sawtell                                           |
  159.  | SMAIL  215 Ollivier's Road, Linwood, Christchurch, 8001. New Zealand.|
  160.  | EMAIL  chris@gerty.equinox.gen.nz                                    |
  161.  | PHONE  +64-3-389-3200   ( gmt +13 - your discretion is requested )   |
  162.  +----------------------------------------------------------------------+
  163.